import { Injectable } from '@nestjs/common';
import { CreatePermissionDto } from '../dto/create-permission.dto';
import { UpdatePermissionDto } from '../dto/update-permission.dto';
import { Permission } from '../entity/permission.entity';

@Injectable()
export class PermissionService {
  create(createPermissionDto: CreatePermissionDto) {
    return Permission.save({
      ...createPermissionDto,
    });
  }

  async findAll(pageNum: number, pageSize: number) {
    const [list, count] = await Permission.findAndCount({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
      order: {
        createTime: 'DESC',
      },
    });

    return { list, count };
  }

  findOne(id: number) {
    return Permission.findOneBy({ id });
  }

  update(id: number, updatePermissionDto: UpdatePermissionDto) {
    return Permission.update(id, { ...updatePermissionDto });
  }

  remove(id: number) {
    return Permission.update(id, { deleteTime: new Date() });
  }
}
